********Figure M.1 - Mexico********

*Load data and create differences
cd "$reploc/data/created_data"
use mex_full, clear

sort pid_link mxfls
drop if mxfls==.
keep if panel==1

keep rbuc* mxfls pid_link inflation imp_age panel birthyr grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co? grbrprovSD_lam0co? birthstate yearprov industry

reshape wide rbuc*  inflation imp_age panel grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co? grbrprovSD_lam0co? industry, i(pid_link) j(mxfls)


local varlist1 rbuc rbuc_nga inflation grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co2 grbrprovSD_lam0co2 grbrprovAvg_lam0co3 grbrprovSD_lam0co3 industry


foreach var in `varlist1'  {
gen `var'dif=.
replace `var'dif=`var'3-`var'2
}

*Generate cohort by birthyear variables to cluster on
gen coh_prov_clust=yearprov

*Regressions
local x rbuc_nga
local rhsvari oldmexonly
local rhsvaruse _lam0co3dif
local usei grbr
local ind industry2

drop if `x'dif==.

reg `x'dif `usei'provAvg`rhsvaruse' `usei'provSD`rhsvaruse'  inflationdif c.`usei'provAvg`rhsvaruse'#ib3.`ind' c.`usei'provSD`rhsvaruse'#ib3.`ind', vce(cluster coh_prov_clust)

cd "$reploc/results"	
regsave using mex_industry.dta, tstat pval ci level(90) replace

*Graphs

cd "$reploc/results"	
use mex_industry.dta, clear

local measures Avg SD

foreach measu in `measures' {

preserve

local is 1 2 4 5 6 7 8 9

gen varinterest=0
foreach i in `is'{
replace varinterest=1 if var=="`i'.industry4#c.grbrprovAvg_lam0co3dif" | var=="`i'.industry2#c.grbrprovAvg_lam0co3dif"
replace varinterest=2 if var=="`i'.industry4#c.grbrprovSD_lam0co3dif" | var=="`i'.industry2#c.grbrprovSD_lam0co3dif"
}

gen locat=.
replace locat=1 if var=="1.industry4#c.grbrprovAvg_lam0co3dif" | var=="1.industry2#c.grbrprovAvg_lam0co3dif"
replace locat=3 if var=="1.industry4#c.grbrprovSD_lam0co3dif" | var=="1.industry2#c.grbrprovSD_lam0co3dif"
replace locat=6 if var=="2.industry4#c.grbrprovAvg_lam0co3dif" | var=="2.industry2#c.grbrprovAvg_lam0co3dif"
replace locat=8 if var=="2.industry4#c.grbrprovSD_lam0co3dif" | var=="2.industry2#c.grbrprovSD_lam0co3dif"
replace locat=11 if var=="4.industry4#c.grbrprovAvg_lam0co3dif" | var=="4.industry2#c.grbrprovAvg_lam0co3dif"
replace locat=13 if var=="4.industry4#c.grbrprovSD_lam0co3dif" | var=="4.industry2#c.grbrprovSD_lam0co3dif"
replace locat=16 if var=="5.industry4#c.grbrprovAvg_lam0co3dif" | var=="5.industry2#c.grbrprovAvg_lam0co3dif"
replace locat=18 if var=="5.industry4#c.grbrprovSD_lam0co3dif" | var=="5.industry2#c.grbrprovSD_lam0co3dif"
replace locat=21 if var=="6.industry4#c.grbrprovAvg_lam0co3dif" | var=="6.industry2#c.grbrprovAvg_lam0co3dif"
replace locat=23 if var=="6.industry4#c.grbrprovSD_lam0co3dif" | var=="6.industry2#c.grbrprovSD_lam0co3dif"
replace locat=26 if var=="7.industry4#c.grbrprovAvg_lam0co3dif" | var=="7.industry2#c.grbrprovAvg_lam0co3dif"
replace locat=28 if var=="7.industry4#c.grbrprovSD_lam0co3dif" | var=="7.industry2#c.grbrprovSD_lam0co3dif"
replace locat=31 if var=="8.industry4#c.grbrprovAvg_lam0co3dif" | var=="8.industry2#c.grbrprovAvg_lam0co3dif"
replace locat=33 if var=="8.industry4#c.grbrprovSD_lam0co3dif" | var=="8.industry2#c.grbrprovSD_lam0co3dif"
replace locat=36 if var=="9.industry4#c.grbrprovAvg_lam0co3dif" | var=="9.industry2#c.grbrprovAvg_lam0co3dif"
replace locat=38 if var=="9.industry4#c.grbrprovSD_lam0co3dif" | var=="9.industry2#c.grbrprovSD_lam0co3dif"

capture label drop locatlab
label define locatlab 2 "Agriculture" 7 "Mining" 12 "Utilities" 17 "Construction" 22 "Wholesale & Retail" 27 "Transportation" 32 "Finance" 37 "Social Services" 

local tit "Heterogeneity of Main Resuls by Industry"
local numpl 8
label values locat locatlab

if "`measu'"=="Avg" {
local imi=1
local col "maroon"
}


if "`measu'"=="SD" {
local imi=2
local col "gold"
}

twoway (scatter locat coef if varinterest==`imi', mcolor(`col')) (rcap ci_lower ci_upper locat if varinterest==`imi', lcolor(`col') horizontal) ///
, legend(off) graphregion(color(white)) xline(0) ylabel(2 7 12 17 22 27 32 37, valuelabel labsize(vsmall) noticks angle(horizontal)) ///
ytitle("Industry (Baseline Category: Manufacturing)", size(vsmall))

graph export FigureM1Mexico`measu'.eps, replace



restore

}
